Jika saya memiliki batasan kunci asing dari tabel itu sendiri, apakah saya perlu berhati-hati saat menghapus seluruh tabel? (If I have a foreign key constraint of a table to itself, do I need to be careful when deleting the whole table?)


問題描述

Jika saya memiliki batasan kunci asing dari tabel itu sendiri, apakah saya perlu berhati‑hati saat menghapus seluruh tabel? (If I have a foreign key constraint of a table to itself, do I need to be careful when deleting the whole table?)

I have a SQLite table that looks like this:

CREATE TABLE myTable (
    _id integer primary key autoincrement,
    ...
    ... /* some useful fields */
    ...
    parent integer,
    FOREIGN KEY (parent) REFERENCES myTable (_id)
);

Some of the entries in myTable have a parent, others don't. Business logic ensures that there are no "circles". I'm using PRAGMA foreign_keys = ON, so that foreign key violations are detected.

Question: If I want to delete the whole table, is it enough to execute DELETE FROM myTable? Or do I need to delete the "leaves" first, then their parents, etc., all the way up to avoid foreign key violations?

I've tested it and using just DELETE FROM myTable seems to work. However, I want to know whether this is just a coincidence or something I can rely upon (i.e. documented and expected behaviour).


EDIT: I don't have ON DELETE CASCADE enabled, and this is on purpose.

‑‑‑‑‑

參考解法

方法 1:

Insert

By specifying a foreign key and with PRAGMA foreign_keys = ON, starting from an empty table, SQLite will deny you any entry of data (except identities, where parent = _id) with the message:

  

Error: foreign key constraint failed.

So from the start, you need to make at least one entry with PRAGMA foreign_keys = OFF or a "circular" entry.

Deletion of specific entries

If you do not provide an action such as CASCADE to your foreign key, SQLite will deny deletion of a "parent entry" if the parent has children due to the foreign key constraint. This is the default behaviour, equivalent to "NO ACTION".

Deletion of all entries

Deletion of all entries is possible through DELETE FROM myTable and should not trigger the foreign key constraints on the same table, even if there is no cascade clause. I verified by test, it might not be documented.

(by HeinziMPelletier)

參考文件

  1. If I have a foreign key constraint of a table to itself, do I need to be careful when deleting the whole table? (CC BY‑SA 3.0/4.0)

#referential-integrity #foreign-keys #sqlite






相關問題

Jika saya memiliki batasan kunci asing dari tabel itu sendiri, apakah saya perlu berhati-hati saat menghapus seluruh tabel? (If I have a foreign key constraint of a table to itself, do I need to be careful when deleting the whole table?)

如何檢查我是否只刪除了所需的數據? (How do I check that I removed required data only?)

如何在 Postgres 8.2 中禁用參照完整性? (How do I disable referential integrity in Postgres 8.2?)

Xóa phụ huynh nếu nó không được tham chiếu bởi bất kỳ đứa trẻ nào khác (Delete parent if it's not referenced by any other child)

Có cách nào để kiểm tra tính toàn vẹn của tham chiếu cho các bảng MyIsam bằng cách sử dụng quan hệ gốc YII không? (Is there a way to check referential integrity for MyIsam tables using YII native relations?)

ActiveDirectoryMembershipProvider 和參照完整性 (ActiveDirectoryMembershipProvider and referential integrity)

SQL2005:將一個錶鍊接到多個表並保留Ref Integrity? (SQL2005: Linking a table to multiple tables and retaining Ref Integrity?)

違反完整性約束 - 調用存儲過程時未找到父鍵 (Integrity constraint violated - parent key not found when calling stored procedure)

db2 參照完整性問題 (db2 referential integrity problem)

無法在 Access 中強制執行參照完整性 (unable to enforce referential integrity in Access)

破壞的參照完整性:埃德加科德會說什麼? (Broken referential integrity: What would Edgar Codd say?)

如何更新鏈接到多個表的 FK - 更新時的級聯 (How to update FK linked to multiple table - Cascade on Update)







留言討論